package team.seekers.catering.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import team.seekers.catering.common.enums.FoodTypeEnum;
import team.seekers.catering.common.enums.RoleEnum;
import team.seekers.catering.common.utils.CateringUtil;
import team.seekers.catering.entity.dataobject.User;
import team.seekers.catering.entity.params.update.OfflineUserUpdatePA;
import team.seekers.catering.entity.params.update.UserUpdatePA;
import team.seekers.catering.entity.vo.info.UserInfo;
import team.seekers.catering.entity.params.add.UserAddPA;
import team.seekers.catering.handler.exceptionHandler.ResultException;
import team.seekers.catering.mapper.UserMapper;
import team.seekers.catering.service.UserService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * 服务实现类
 *
 * @author cbj
 * @author yiming
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    /**
     * 绑定身体数据成为线下用户
     * @param offlineUserUpdatePA
     */
    @Override
    public void bindOffline(OfflineUserUpdatePA offlineUserUpdatePA) {
        String userId = CateringUtil.getUserId();
        User user = getById(userId);
        if(user == null){
            throw new ResultException().message("你已掉线，请重新登录");
        }
        BeanUtils.copyProperties(offlineUserUpdatePA, user);

        BigDecimal bmi = new BigDecimal(offlineUserUpdatePA.getWeight()/(offlineUserUpdatePA.getHeight()*offlineUserUpdatePA.getHeight())*100000);
        user.setBmi(bmi);
        user.setRole(RoleEnum.USER_OFFLINE);

        // TODO 根据身体数据计算推送的套餐种类
        user.setFoodType(FoodTypeEnum.BALANCE_MAN);

        boolean b = updateById(user);
        if(!b){
            throw new ResultException().message("注册失败，请稍后重试");
        }
    }

    /**
     * 绑定身体数据成为线上用户
     * @param offlineUserUpdatePA
     */
    @Override
    public void bindOnline(OfflineUserUpdatePA offlineUserUpdatePA) {
        String userId = CateringUtil.getUserId();
        User user = getById(userId);
        if(user == null){
            throw new ResultException().message("你已掉线，请重新登录");
        }
        BeanUtils.copyProperties(offlineUserUpdatePA, user);

        BigDecimal bmi = new BigDecimal(offlineUserUpdatePA.getWeight()/(offlineUserUpdatePA.getHeight()*offlineUserUpdatePA.getHeight())*100000);
        user.setBmi(bmi);
        user.setRole(RoleEnum.USER_ONLINE);
        boolean b = updateById(user);
        if(!b){
            throw new ResultException().message("注册失败，请稍后重试");
        }
    }

    /**
     * 渲染个人信息
     */
    @Override
    public UserInfo get() {
        String userId = CateringUtil.getUserId();
        UserInfo userInfo = new UserInfo();

        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("id", userId);
        User user = baseMapper.selectOne(wrapper);
        if(user == null){
            throw new ResultException().message("你已掉线，请重新登录");
        }
        BeanUtils.copyProperties(user, userInfo);

        return userInfo;
    }

    @Override
    public void update(UserUpdatePA userUpdatePA) {
        return;
    }

}
